// pages/my/index.js
import { bannerList } from "../../api/equity/index"
import { updateInfo } from "../../api/my/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    name:'',
    nickname:'',
    logo:'',
    carName:''
  },
  handleOrder(event){
    console.log(event)
    wx.navigateTo({
      url: '/pages/my/order/index?type=' + event.target.dataset.type,
    })
  },
  getBannerList(){
    bannerList().then(res=>{
      console.log(res)
      this.setData({
        list:res.data.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(app)
    this.getBannerList()
    this.setData({
      avatar:app.globalData.userInfo.avatar,
      nickname:app.globalData.userInfo.nickname,
      phone:app.globalData.userInfo.phone,
      logo:app.globalData.isHaveCar ?app.globalData.isHaveCar.carMark :'',
      carName:app.globalData.isHaveCar ? app.globalData.isHaveCar.carNo : ''
    })
  },
  handleEdit(){
    this.setData({
      show:true
    })
  },
  updateName(){
    updateInfo({
      nickname:this.data.name
    }).then(res=>{
      app.globalData.userInfo.nickname = this.data.name
        this.setData({
          nickname:this.data.name,
          show:false,
          name:''
        })
        app.globalData.userInfo.nickname = this.data.name

    })
  },
  cancelUpdate(){
    this.setData({
      show:false
    })
  },
  handleChooseImage() {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success(res) {
        wx.uploadFile({
          url: 'https://chezhu.prd4.weiniaokeji.cn/oss/file', //仅为示例，非真实的接口地址
          filePath:res.tempFiles[0].tempFilePath,
          name: 'file',
          success(res) {
            console.log(res)
            const data = JSON.parse(res.data) 
            console.log(data)
            //do something
            updateInfo({
              avatar:data.data
            }).then(res=>{
              app.globalData.userInfo.avatar = data.data
                this.setData({
                  show:false,
                  avatar:data.data
                })
        
            })
          }
        })
      }
    })
  },
  loginout(){
    console.log(1)
    app.globalData.userInfo = null
    app.globalData.isHaveCar = null
    wx.reLaunch({
      url: '/pages/authorize/login/login',
    })
  },
  goMyCar(){
    wx.navigateTo({
      url: '/pages/my/car/index',
    })
  },
  goStore(){
    wx.navigateTo({
      url: '/pages/store/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})